<?php

namespace App\Service;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Area extends Model
{

    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        $this->request = $request;
        $model = $this;
        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        $model=$model->paginate($this->request->limit)
            ->toArray();
        return $model;
    }


    public static function getDisplay($length=4){
        $res = self::where('status',self::DISPLAY)->limit(4)->get();
        if($res){
            return $res->toArray();
        }else{
            return [];
        }
    }
    /**
     * @param $value
     *
     * @return mixed
     */
    public function getStatusAttribute($value){
        $config= $this->getStatusList();
        return $config[$value];
    }

    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->delete();
            }else{
                $this->where('id',$id)->delete();

            }
        }
    }


    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $old = self::find($post['id']);
            $old->name = $post['name'];
            $old->content = isset($post['content'])?$post['content']:'';
            $old->sort = $post['sort']?$post['sort']:50;
            $old->save();
        }else{
            $this->name = $post['name'];
            $this->content = isset($post['content'])?$post['content']:'';
            $this->sort = $post['sort']?$post['sort']:50;
            $this->save();
        }
    }

}
